﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Xml.Linq;

namespace TransformeXmlEnCsv
{
	public class Composant
	{
		#region Attributs
		/// <summary>
		/// ID de l'item produit
		/// </summary>
		public int IdProductType { get; set; }

		/// <summary>
		/// Quantité nécessaire pour la fabrication.
		/// </summary>
		public int Quantite { get; set; }

		/// <summary>
		/// ID du composant utilisé.
		/// </summary>
		public int IdComposant { get; set; }

		/// <summary>
		/// Pourcentage de dommage en fabrication (1 = 100%).
		/// </summary>
		public double DamagePerJob { get; set; }

		/// <summary>
		/// J'en sais rien.
		/// </summary>
		public byte Wasted { get; set; }

		/// <summary>
		/// Identifiant de l'activité (1 = Manufacture, 8 = Invention)
		/// </summary>
		public int ActivityId { get; set; }

		#endregion

		#region Constructeur

		/// <summary>
		/// 
		/// </summary>
		/// <param name="elementComposant"></param>
		/// <param name="idBluePrintItemConstruit"></param>
		public Composant(XElement elementComposant, int idBluePrintItemConstruit)
		{
			// Exemples :
			// <m id="34" quantity="60" damagePerJob="1" activityId="1" wasted="1" />
            // <m id="35" quantity="20" damagePerJob="1" activityId="1" wasted="1" />

			// Cherchons les informations dans les attributs de l'élément.
			var infoBp = (from dd in elementComposant.Attributes()
						  select dd);

			IdProductType = idBluePrintItemConstruit;
			Quantite = Convert.ToInt32(infoBp.Where(y => y.Name == "quantity").Select(x => x.Value).First());
			IdComposant = Convert.ToInt32(infoBp.Where(y => y.Name == "id").Select(x => x.Value).First());
			DamagePerJob = Double.Parse(infoBp.Where(y => y.Name == "damagePerJob").Select(x => x.Value).First(), new System.Globalization.CultureInfo("en-US", false));
			Wasted = Convert.ToByte(infoBp.Where(y => y.Name == "wasted").Select(x => x.Value).First());
			ActivityId = Convert.ToByte(infoBp.Where(y => y.Name == "activityId").Select(x => x.Value).First());
		}

		#endregion

		public override string ToString()
		{
			const char VIRGULE = ';';
			StringBuilder sb = new StringBuilder();
			sb.Append(IdProductType);
			sb.Append(VIRGULE);
			sb.Append(IdComposant);
			sb.Append(VIRGULE);
			sb.Append(Quantite);
			sb.Append(VIRGULE);
			sb.Append(Convert.ToString(DamagePerJob, new CultureInfo("en-US")));
			sb.Append(VIRGULE);
			sb.Append(Wasted);
			sb.Append(VIRGULE);
			sb.Append(ActivityId);
			sb.Append('|');

			return sb.ToString();

		}

	}
}
